package cn.ym.controller;

import cn.ym.common.AjaxResult;
import cn.ym.common.IHttpMessage;
import cn.ym.common.utils.OkHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;


/**
 * @ProjectName: SprintgbootCuteCat
 * @Package: cn.ym.controller
 * @ClassName: MessageController
 * @Author: yiming
 * @Description: 消息接收器
 * @Date: 2022/2/16 10:09
 * @Version: 1.0
 */
@RestController
@RequestMapping("/debug")
public class MessageController {

    private static final Logger LOG = LoggerFactory.getLogger(MessageController.class);

    /**
     * 同下
     * @param iHttpMessage
     * @return
     */
    @GetMapping
    public AjaxResult messageGet(@RequestBody IHttpMessage iHttpMessage)
    {
        if(null!=iHttpMessage)
        {
            LOG.info(iHttpMessage.toString());
        }
        return AjaxResult.success();
    }

    /**
     * 可爱猫发过来的是json格式的数据，使用@RequestBody 接受
     * @param iHttpMessage
     * @return
     */
    @PostMapping
    public AjaxResult messagePost(@RequestBody IHttpMessage iHttpMessage) throws IOException
    {
        if(null!=iHttpMessage)
        {
            LOG.info(iHttpMessage.toString());
            // 如果消息来源不同
            if(!iHttpMessage.getFinal_from_wxid().equalsIgnoreCase(iHttpMessage.getRobot_wxid()))
            {
                // 简易转发转发，如项目使用请自己封装~
                OkHttpUtil.sendMessage(iHttpMessage.getRobot_wxid(),iHttpMessage.getMsg());

            }
            return AjaxResult.success();
        }
        else
        {
            LOG.info("------消息为空-----非法请求");
            return AjaxResult.error();
        }
    }



}
